package com.hzy.quetosys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzy.quetosys.dao.CustomerMapper;
import com.hzy.quetosys.dao.ProductMapper;
import com.hzy.quetosys.dao.ProductTypeMapper;
import com.hzy.quetosys.entity.Customer;
import com.hzy.quetosys.entity.Product;
import com.hzy.quetosys.entity.ProductType;
import com.hzy.quetosys.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductTypeServiceImpl implements ProductTypeService {
    @Autowired
    private ProductTypeMapper productTypeMapper;
    @Autowired
    private ProductMapper productMapper;


//    @Override
//    public PageInfo<ProductType> findAll(int pageNum, int pageSize) {
//        PageHelper.startPage(pageNum,pageSize);
//        final List<ProductType> productTypes = productTypeMapper.selectAll();
//        PageInfo<ProductType> pageInfo=new PageInfo<>(productTypes);
//        return pageInfo;
//    }

    @Override
    public List<ProductType> findAll() {
        return productTypeMapper.selectAll();
    }

    @Override
    public PageInfo<ProductType> findByKeyword(int pageNum, int pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        List<ProductType> productTypes=productTypeMapper.selectByKeyword(keyword);
        PageInfo<ProductType> pageInfo=new PageInfo<>(productTypes);
        return pageInfo;
    }

    @Override
    public ProductType findById(int id) {
        return productTypeMapper.selectById(id);
    }

    @Override
    public boolean modify(ProductType productType) {
        return productTypeMapper.updateById(productType)>0;
    }

    @Override
    public boolean append(ProductType productType) {

            return productTypeMapper.insert(productType) > 0;
        }

    /**
     * 根据id删除服务类型信息
     * @param id
     * @return
     */
    @Transactional(rollbackFor =Exception.class)
        @Override
        public boolean remove ( int id){
            //id=-1
           int result= productMapper.updateTypeIdByTypeId(id);

           if(result>0) {
               //产品类型正确修改，才能删除该类型
               return productTypeMapper.deleteById(id) > 0;
           }
           return result>0;
        }
    }

